/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.demo.notepad2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class NoteEdit extends Activity {

	private EditText mTitleText;
	private EditText mDateText;
    private EditText mTimeStartText;
    private EditText mTimeEndText;
    private EditText mLocationText;
    private EditText mDescriptionText;
    private Button confirmButton;
    private Spinner spinner;
    private Long mRowId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_edit);
        setTitle(R.string.edit_note);
 
        mTitleText = (EditText) findViewById(R.id.title);
        mDateText = (EditText) findViewById(R.id.date);
        mTimeStartText = (EditText) findViewById(R.id.time1);
        mTimeEndText = (EditText) findViewById(R.id.time2);
        mLocationText = (EditText) findViewById(R.id.location);
        mDescriptionText = (EditText) findViewById(R.id.description);

        confirmButton = (Button) findViewById(R.id.confirm);
 

        spinner = (Spinner) findViewById(R.id.spinner1);
        
     // Create an ArrayAdapter using the string array and a default spinner layout
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
             R.array.categories_array, android.R.layout.simple_spinner_item);
     // Specify the layout to use when the list of choices appears
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     // Apply the adapter to the spinner
     spinner.setAdapter(adapter);
     
        mRowId = null;
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String title = extras.getString(NotesDbAdapter.KEY_TITLE);
            String date = extras.getString(NotesDbAdapter.KEY_DATE);
            String time_start = extras.getString(NotesDbAdapter.KEY_TIME_START);
            String time_end = extras.getString(NotesDbAdapter.KEY_TIME_END);
            String location = extras.getString(NotesDbAdapter.KEY_LOCATION);
            String categories = extras.getString(NotesDbAdapter.KEY_CATEGORIES);
            String description = extras.getString(NotesDbAdapter.KEY_DESCRIPTION);
            
            mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);

            if (title != null) {
                mTitleText.setText(title);
            }
            if (date != null) {
                mDateText.setText(date);
            }
            if (time_start != null) {
            	mTimeStartText.setText(time_start);
            }
            if (time_end != null) {
            	mTimeEndText.setText(time_end);
            }
            if (location != null) {
            	mLocationText.setText(location);
            }
            // idk
            if (categories != null) {
            	for (int i=0;;i++){
            		spinner.setSelection(i);
            		if (spinner.getSelectedItem().equals(categories)){
            			break;
            		}
            	}
            }
            if (description != null) {
            	mDescriptionText.setText(description);
            }
        }

        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Bundle bundle = new Bundle();

                bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText().toString());
                bundle.putString(NotesDbAdapter.KEY_DATE, mDateText.getText().toString());
                bundle.putString(NotesDbAdapter.KEY_TIME_START, mTimeStartText.getText().toString());
                bundle.putString(NotesDbAdapter.KEY_TIME_END, mTimeEndText.getText().toString());
                bundle.putString(NotesDbAdapter.KEY_LOCATION, mLocationText.getText().toString());
                bundle.putString(NotesDbAdapter.KEY_CATEGORIES, spinner.getSelectedItem().toString());
                bundle.putString(NotesDbAdapter.KEY_DESCRIPTION, mDescriptionText.getText().toString());
                if (mRowId != null) {
                    bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
                }

                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
                finish();
            }

        });
    }
}
